<?php

namespace api\controller;

// +----------------------------------------------------------------------
// | 集团公司申请数据控制层
// +----------------------------------------------------------------------

use common\controller\VerifyController;
use common\exception\LogicException;
use common\service\TenantGroupApplyService;
use think\Request;

class TenantGroupApplyController extends VerifyController
{
    protected $tenantGroupApplyService;

    public function __construct()
    {
        parent::__construct();
        $this->tenantGroupApplyService = new TenantGroupApplyService();
    }

    /**
     * 查询申请列表
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function select(Request $request)
    {
        try {
            $param = $request->param();
            $resData = $this->tenantGroupApplyService->select($param, $request->getTenantId());
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 发起申请
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function sendApply(Request $request)
    {
        try {
            $param = $request->param();
            $resData = $this->tenantGroupApplyService->sendApply($param['data'], $request->getUserId(), $request->getTenantId());
            return json(success_response('Success', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 审核申请
     * @param Request $request
     * @return \think\Response
     * @throws \Throwable
     */
    public function auditApply(Request $request)
    {
        try {
            $param = $request->param();
            $resData = $this->tenantGroupApplyService->auditApply($param['data'], $request->getUserId());
            return json(success_response('Success', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 当前租户查找申请集团
     * @param Request $request
     * @return \think\Response
     */
    public function selectCurrentTenant(Request $request)
    {
        try {
            $resData = $this->tenantGroupApplyService->selectCurrentTenant($request->getTenantId());
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 重新激活申请状态接口
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function activate(Request $request)
    {
        try {
            $param = $request->param();
            $resData = $this->tenantGroupApplyService->activate($param['data'], $request->getUserId());
            return json(success_response('Success', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }
}